// ==UserScript==
// @name         FA快速复制内容
// @namespace    http://tampermonkey.net/
// @version      0.0.3
// @description  快速复制 FA 的内容
// @author       HqLin
// @match        https://www.furaffinity.net/view/*
// @grant        GM_setClipboard
// ==/UserScript==
'use strict';

(function () {
    let selectors = {
        "preview": "center div",
        "intro": ".submission-description.user-submitted-links",
        "url": document.URL,
        "tags": ".tags-row span",
        "title": ".submission-title p"
    }
    let ui = `
  <select class="fac-copy" class="top-heading" style="display: inline-block;height: 100%;outline: none;">
    <option value="">选择</option>
    <option value="${selectors.preview}">预览</option>
    <option value="${selectors.intro}">介绍</option>
    <option value="${selectors.url}">链接</option>
    <option value="${selectors.tags}">标签</option>
    <option value="${selectors.title}">标题</option>
  </select>
  `
    let pc = document.createElement("li")
    pc.classList.add("lileft")
    pc.style.height = "100%"
    pc.style.verticalAlign = "middle"
    pc.innerHTML = ui
    document.querySelector(".navhideonmobile")?.appendChild(pc)

    let mob = document.createElement("div")
    mob.style.height = "40px"
    mob.style.display = "flex"
    mob.style.alignItems = "center"
    mob.innerHTML = ui
    document.querySelector(".mobile-nav-container-item.right")?.appendChild(mob)

    let uidoms: NodeListOf<Element> = document.querySelectorAll(".fac-copy")
    for (const uidom of uidoms) {
        (uidom as HTMLSelectElement)?.addEventListener<"change">("change", event => {
            const sel = (event?.target as HTMLSelectElement)?.value
            switch (sel) {
                case selectors.preview: {
                    const txt = document.querySelector(sel)?.textContent?.trim()
                    GM_setClipboard(txt?.split(/-{10,}/g).join(";;") ?? "")
                    break
                }
                case selectors.title: {
                    const txt = document.querySelector(sel)?.textContent?.trim()
                    GM_setClipboard(txt?.replace(/[|\[\]\{\}]/g, "") ?? "")
                    break
                }
                case selectors.url: {
                    GM_setClipboard(selectors.url)
                    break
                }
                case selectors.tags: {
                    let res = ``
                    for (const tag of document.querySelectorAll(".tags-row span")) {
                        const tagval = tag.textContent?.trim()
                        if (tagval?.includes(" ")) {
                            res = `${res} [[${tagval}]]`
                        } else {
                            res = `${res} ${tagval}`
                        }
                    }
                    GM_setClipboard(res)
                    break
                }
                default: {
                    const txt = document.querySelector(sel)?.textContent?.trim()
                    GM_setClipboard(txt ?? "")
                }
            }
        })
    }
})();
